package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.PopupDiscountSelector;
import com.yunkaweilai.android.model.member.PreferentiaActivitiesModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import razerdp.basepopup.BasePopupWindow;

/**
 * 收款优惠活动弹窗
 * Created by Administrator on 2017/7/17.
 */

public class PreferentialActivitiesPopup extends BasePopupWindow{

    private View popupView;
    private Activity mContext;
    private ArrayList<PreferentiaActivitiesModel.DataBean.ListBean> list = new ArrayList<>();
    private CommonAdapter<PreferentiaActivitiesModel.DataBean.ListBean> adapter;
    private PopupDiscountSelector popupDiscountSelector;
    private ListView idListView;
    private String id;
    private TextView idTvClean;
    private String price;

    public PreferentialActivitiesPopup(Activity context,String id,String price) {
        super(context);
        this.id = id;
        this.mContext = context;
        this.price = price;
        bindEvent();
    }
    public void setPopupDiscountSelector(PopupDiscountSelector popupDiscountSelector){
        this.popupDiscountSelector = popupDiscountSelector;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_preferential_activities, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {
        initAdapter();
        initHttp();
        if (popupView != null) {
            idTvClean = ((TextView) popupView.findViewById(R.id.id_tv_clean));
            idListView = ((ListView) popupView.findViewById(R.id.id_listview));
            idListView.setAdapter(adapter);
            idListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (list.get(position).is_click()){
                        popupDiscountSelector.updateInfo(" -"+list.get(position).getDiscount_content().getFree_price()
                                ,list.get(position).getTitle(),list.get(position).getId());
                        dismiss();
                    }else{
                        MyToast.showShortToast(mContext,"无法参加该活动");
                    }

                }
            });
            idTvClean.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupDiscountSelector.updateInfo("","","");
                    dismiss();
                }
            });
        }

    }
    //获取优惠活动
    private void initHttp() {
        HttpRequest.postUrl(Api.OPERATION_DISCOUNT_ACTIVITY)
                .addParams("member_id",id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            PreferentiaActivitiesModel preferentiaActivitiesModel = gson.fromJson(json, PreferentiaActivitiesModel.class);
                            list.clear();
                            list.addAll(preferentiaActivitiesModel.getData().getList());
                            for (int i = 0; i < list.size(); i++) {
                                PreferentiaActivitiesModel.DataBean.ListBean.DiscountContentBean discount_content = list.get(i).getDiscount_content();
                                if (Float.parseFloat(discount_content.getFull_price()) <= Float.parseFloat(price)){
                                    list.get(i).setIs_click(true);
                                }else{
                                    list.get(i).setIs_click(false);
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

    private void initAdapter() {
        adapter = new CommonAdapter<PreferentiaActivitiesModel.DataBean.ListBean>(mContext,R.layout.item_list_popup_discount,list) {
            @Override
            protected void convert(ViewHolder viewHolder, PreferentiaActivitiesModel.DataBean.ListBean item, int position) {
                TextView idTvTypeName = viewHolder.getView(R.id.id_tv_type_name);
                TextView idTvtime = viewHolder.getView(R.id.id_tv_time);
                viewHolder.setText(R.id.id_tv_type_name, item.getTitle() + " (" + item.getDiscount_contents() + ")");
                viewHolder.setText(R.id.id_tv_time,"活动时间：" + item.getDate_type_contents());
                if (item.is_click()){
                    idTvTypeName.setTextColor(mContext.getResources().getColor(R.color.wjx_content_txt));
                    idTvtime.setTextColor(mContext.getResources().getColor(R.color.wjx_content_txt));
                }else{
                    idTvTypeName.setTextColor(mContext.getResources().getColor(R.color.wjx_content_txt_grey));
                    idTvtime.setTextColor(mContext.getResources().getColor(R.color.wjx_content_txt_grey));
                }
            }
        };
    }

}
